package AdminClient;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.SwingUtilities;
import java.util.*;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AdminGUI extends javax.swing.JFrame {
	private JPanel panel;
	private JButton logoff;
	private JButton log;
	private JButton help;
	private JButton worldview;
	private JScrollPane jScrollPane5;
	private JLabel operationslabel;
	private JLabel welcomeMessage;
	private JList operationslist;
	private JScrollPane jScrollPane4;
	private JList itemlist;
	private JScrollPane jScrollPane2;
	private JLabel connectionslabel;
	private JList connectionslist;
	private JScrollPane jScrollPane3;
	private JScrollPane jScrollPane1;
	private JTextField displaytextfield;
	private JTextField username;
	private JTextField password;
	private JLabel playerlabel;
	private JList playerlist;
	private JLabel itemlabel;
	private JLabel roomlabel;
	private JList roomlist;
	private String[] defaultoperations;
	private String[] roomselops;
	private String[] playerselops;
	private String[] itemselops;
	private String[] connselops;
	private String roomselected;
	private String itemselected;
	private String playerselected;
	private String connectionselected;
	private boolean addconn;
	private ListModel operationslistModel;
	private ListModel roomlistModel;
	private ListModel itemlistModel;
	private ListModel playerlistModel;
	private ListModel connectionslistModel;
	private String[] itemsinroom;
	private String[] playersinroom;
	private String[] connsfromroom;
	private String[] playersitems;
	private String[] getrooms;
	private String[] getitems;
	private String[] getplayers;
	private String correctusername="Jimmy";
	private String correctpassword="frenchtoast";
	private AdminInfo ai;

	/**
	* Auto-generated main method to display this JFrame
	*/
/*	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AdminGUI inst = new AdminGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	*/
	public AdminGUI(AdminInfo a) {
		super();
		ai=a;
		defaultoperations = new String[] {"Add Room", "Chat with World"};
		roomselops=new String[] {"Modify Room", "Remove Room", "Chat with Room", "Add Item","Add Connection", "Add Connection to Other World"};
		playerselops=new String[] {"Chat with Player"};
		itemselops=new String[] {"Modify Item", "Remove Item", "Lock Item", "Unlock Item"};
		connselops=new String[] {"Remove Connection"};
		roomselected=" ";
		itemselected=" ";
		playerselected=" ";
		connectionselected=" ";
		addconn = false;
		operationslistModel=null;
		roomlistModel=null;
		itemlistModel=null;
		playerlistModel=null;
		connectionslistModel=null;
		itemsinroom= new String[] {"Alpha", "Beta", "Gamma", "Delta", "Epsilon"};
		playersinroom= new String[] {"Zeta", "Eta", "Theta", "Iota", "Kappa", "Lambda"};
		connsfromroom=new String[] {"Phi", "Chi", "Psi", "Omega"};
		playersitems=new String[] {"Pi", "Rho", "Sigma"};
		getrooms=new String[] {"Room One", "Room Two", "Room Three", "Room Four", "Room Five", "Room Six" };
		getitems=new String[] { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" };
		getplayers=new String[] { "Player One", "Player Two", "Player Three", "Player Four", "Player Five" };
		initGUI();
	}
	private void initGUI()
	{
		try
		{
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				getContentPane().removeAll();
				panel=new JPanel();
				getContentPane().add(panel, BorderLayout.CENTER);
				panel.setLayout(null);
				panel.setPreferredSize(new java.awt.Dimension(407,296));
				{
					{
						welcomeMessage=new JLabel();
						getContentPane().add(welcomeMessage, BorderLayout.NORTH);
						welcomeMessage.setText("Welcome to SMUVE Admin Client.\nPlease Log In:");
					}
					{
						username = new JTextField();
						panel.add(username);
						username.setText("Username here");
						username.setBounds(25, 80, 354, 50);
					}
					{
						password = new JTextField();
						panel.add(password);
						password.setText("Password here");
						password.setBounds(25, 180, 354, 50);
					}
					log=new JButton();
					panel.add(log);
					log.setText("Log On");
					log.setBounds(176, 255, 87, 21);
					log.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent evt)
						{
							//if((correctusername.compareTo(username.getText())==0)&&(correctpassword.compareTo(password.getText())==0))
							//{
								loggedInGUI();
							//}
						}
					});
				}
			}
		pack();
		this.setSize(420, 348);
		}catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	private void loggedInGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				getContentPane().removeAll();
				panel = new JPanel();
				getContentPane().add(panel, BorderLayout.CENTER);
				panel.setLayout(null);
				panel.setPreferredSize(new java.awt.Dimension(407, 296));
				{
					jScrollPane1 = new JScrollPane();
					panel.add(jScrollPane1);
					jScrollPane1.setBounds(25, 23, 104, 68);
					{
						roomlistModel = 
							new DefaultComboBoxModel(ai.get_rooms());
						String[] temp=ai.get_rooms();
						roomlist = new JList();
						jScrollPane1.setViewportView(roomlist);
						roomlist.setModel(roomlistModel);
						roomlist.setBounds(25, 23, 93, 68);
						roomlist.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								if(!addconn){
//									System.out.println(roomlist.getSelectedValue());
									roomselected=((String)roomlist.getSelectedValue());
									if(roomselected!=null){
									operationslistModel=new DefaultComboBoxModel(roomselops);
									operationslist.setModel(operationslistModel);
									itemlistModel=new DefaultComboBoxModel(ai.get_room_items(roomselected));
									itemlist.setModel(itemlistModel);
									playerlistModel=new DefaultComboBoxModel(ai.get_room_players(roomselected));
									playerlist.setModel(playerlistModel);
									connectionslistModel=new DefaultComboBoxModel(ai.get_portals(roomselected));
									connectionslist.setModel(connectionslistModel);
									System.out.println("Debog "+roomselected);
									displaytextfield.setText((ai.get_room(roomselected)).get_description());
									}
								}
								else
								{
									String dest=((String)roomlist.getSelectedValue());
									if(dest!=null&&roomselected!=null)
									{
									System.out.println(roomselected+" now connected to "+dest);
									ai.add_local_portal(roomselected, dest);
									System.out.println("get down");
									//ai.add_local_portal(dest, roomselected);
									System.out.println("get down2");
									addconn=false;
									//roomselected=dest;
									}
								}
//								System.out.println("roomlist.valueChanged, event="+evt);
								//TODO add your code for roomlist.valueChanged
							}
						});
					}
				}
				{
					roomlabel = new JLabel();
					panel.add(roomlabel);
					roomlabel.setText("Rooms");
					roomlabel.setBounds(25, 4, 61, 14);
				}
				{
					jScrollPane2 = new JScrollPane();
					panel.add(jScrollPane2);
					jScrollPane2.setBounds(141, 24, 122, 67);
					{
						itemlistModel = 
							new DefaultComboBoxModel(ai.get_items());
						itemlist = new JList();
						jScrollPane2.setViewportView(itemlist);
						itemlist.setModel(itemlistModel);
						itemlist.setBounds(142, 24, 96, 67);
						itemlist.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								itemselected=((String)itemlist.getSelectedValue());
								if(playerselected==null || playerselected.compareTo(" ")==0)
								{
									operationslistModel=new DefaultComboBoxModel(itemselops);
									operationslist.setModel(operationslistModel);
								}
								if(itemselected!=null)
								displaytextfield.setText((ai.get_item(itemselected)).get_description());
//								System.out.println(itemlist.getSelectedValue());
//								System.out.println("itemlist.valueChanged, event="+evt);
								//TODO add your code for itemlist.valueChanged
							}
						});
					}
				}
				{
					itemlabel = new JLabel();
					panel.add(itemlabel);
					itemlabel.setText("Items");
					itemlabel.setBounds(141, 4, 62, 14);
				}
				{
					jScrollPane3 = new JScrollPane();
					panel.add(jScrollPane3);
					jScrollPane3.setBounds(282, 24, 104, 67);
					{
						playerlistModel = 
							new DefaultComboBoxModel(ai.get_players());
						playerlist = new JList();
						jScrollPane3.setViewportView(playerlist);
						playerlist.setModel(playerlistModel);
						playerlist.setBounds(263, 24, 99, 67);
						playerlist.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								playerselected=((String)playerlist.getSelectedValue());
								operationslistModel=new DefaultComboBoxModel(playerselops);
								operationslist.setModel(operationslistModel);
								itemlistModel=new DefaultComboBoxModel(ai.get_player_items(playerselected));
								itemlist.setModel(itemlistModel);
								displaytextfield.setText(" ");
//								System.out.println(playerlist.getSelectedValue());
//								System.out.println("playerlist.valueChanged, event="+evt);
								//TODO add your code for playerlist.valueChanged
							}
						});
					}
				}
				{
					playerlabel = new JLabel();
					panel.add(playerlabel);
					playerlabel.setText("Players");
					playerlabel.setBounds(282, 4, 62, 14);
				}
				{
					displaytextfield = new JTextField();
					panel.add(displaytextfield);
					displaytextfield.setText("This has random bits of information...Yay!");
					displaytextfield.setBounds(25, 193, 354, 50);
				}
				{
					jScrollPane4 = new JScrollPane();
					panel.add(jScrollPane4);
					jScrollPane4.setBounds(25, 125, 99, 56);
					{
						connectionslistModel = 
							new DefaultComboBoxModel(
									new String[] {});
						connectionslist = new JList();
						jScrollPane4.setViewportView(connectionslist);
						connectionslist.setModel(connectionslistModel);
						connectionslist.setBounds(25, 125, 93, 56);
						connectionslist.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								connectionselected=((String)connectionslist.getSelectedValue());
								operationslistModel=new DefaultComboBoxModel(connselops);
								operationslist.setModel(operationslistModel);
								displaytextfield.setText(" ");
//								System.out.println(connectionslist.getSelectedValue());
//								System.out.println("connectionslist.valueChanged, event="+evt);
								//TODO add your code for connectionslist.valueChanged
							}
						});
					}
				}
				{
					connectionslabel = new JLabel();
					panel.add(connectionslabel);
					connectionslabel.setText("Connections");
					connectionslabel.setBounds(25, 105, 93, 14);
				}
				{
					jScrollPane5 = new JScrollPane();
					panel.add(jScrollPane5);
					jScrollPane5.setBounds(142, 125, 244, 56);
					{
						operationslistModel = 
							new DefaultComboBoxModel(defaultoperations);
						operationslist = new JList();
						jScrollPane5.setViewportView(operationslist);
						operationslist.setModel(operationslistModel);
						operationslist.setBounds(142, 128, 234, 80);
						operationslist.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								String sel=((String)operationslist.getSelectedValue());
								if(!(sel==null))
								{
								if(sel.compareTo("Add Room")==0)
								{
									String name=JOptionPane.showInputDialog("Enter Room Name:");
									String desc=JOptionPane.showInputDialog("Enter Room Description:");
									System.out.println(name+" "+desc+" added to world");
									ai.add_room(name, desc);
								}
								else if(sel.compareTo("Chat with World")==0)
								{
									String message=JOptionPane.showInputDialog("Enter Message:");
									System.out.println(message+" sent to everyone");
									//ai.worldChat(message);
								}
								else if(sel.compareTo("Remove Room")==0)
								{
									System.out.println("Removed room "+roomselected);
									ai.remove_room(roomselected);
								}
								else if(sel.compareTo("Modify Room")==0)
								{
									String newname=JOptionPane.showInputDialog("Enter New Room Name:");
									String newdesc=JOptionPane.showInputDialog("Enter New Room Description:");
									System.out.println(roomselected+" data changed to "+newname+" "+newdesc);
									ai.modify_room(roomselected, newname, newdesc);
								}
								/*else if(sel.compareTo("Chat with Room")==0)
								{
									String message=JOptionPane.showInputDialog("Enter message:");
									System.out.println(message+" sent to "+roomselected);
									ai.chat_room(roomselected, message);
								}*/
								else if(sel.compareTo("Add Connection")==0)
								{
									//String to_room=JOptionPane.showInputDialog("Enter to room name:");
									//ai.add_local_portal(roomselected, to_room);
									//ai.add_local_portal(to_room, roomselected);
									addconn=true;
								}
								else if(sel.compareTo("Add Connection to Other World")==0)
								{
									String otherworldroom=JOptionPane.showInputDialog("Enter Room Name from Other World:");
									String hostname=JOptionPane.showInputDialog("Enter host name:");
									int cport=Integer.parseInt(JOptionPane.showInputDialog("Enter cport, whatever that is:"));
									int uport=Integer.parseInt(JOptionPane.showInputDialog("Enter uport, whatever that is:"));
									System.out.println(roomselected+" now connects to "+otherworldroom+" with this other information:\nHostname: "+hostname+"\ncport: "+cport+"\nuport: "+uport);
									ai.add_server_portal(roomselected, otherworldroom, hostname, cport, uport);
								}
								else if(sel.compareTo("Add Item")==0)
								{
									String name=JOptionPane.showInputDialog("Enter Item Name:");
									String desc=JOptionPane.showInputDialog("Enter Item Description:");
									System.out.println("Added "+name+" "+desc);
									ai.add_item(name, roomselected, desc);
								}
								else if(sel.compareTo("Chat with Player")==0)
								{
									String message=JOptionPane.showInputDialog("Enter message:");
									System.out.println(message+" sent to "+playerselected);
									//ai.chat(playerselected, message);
								}
								else if(sel.compareTo("Remove Item")==0)
								{
									System.out.println("Removing: "+ itemselected);
									ai.remove_item(itemselected);
								}
								else if(sel.compareTo("Lock Item")==0)
								{
									System.out.println("Locking: "+ itemselected);
									ai.lock_item(itemselected);
								}
								else if(sel.compareTo("Unlock Item")==0)
								{
									System.out.println("Unocking: "+ itemselected);
									ai.unlock_item(itemselected);
								}
								else if(sel.compareTo("Modify Item")==0)
								{
									String newname=JOptionPane.showInputDialog("Enter New Item Name:");
									String newdesc=JOptionPane.showInputDialog("Enter New Item Description:");
									System.out.println("Changed "+itemselected+" to "+newname+" "+newdesc);
									ai.modify_item(itemselected, newname, newdesc);
								}
								else if(sel.compareTo("Remove Connection")==0)
								{
									System.out.println("Removing: "+connectionselected);
									ai.remove_portal(roomselected, connectionselected);
								}
								}
//								System.out.println(operationslist.getSelectedValue());
//								System.out.println("operationslist.valueChanged, event="+evt);
								//TODO add your code for operationslist.valueChanged
							}
						});
					}
				}
				{
					operationslabel = new JLabel();
					panel.add(operationslabel);
					operationslabel.setText("Operations");
					operationslabel.setBounds(142, 105, 121, 14);
				}
				{
					worldview = new JButton();
					panel.add(worldview);
					worldview.setText("World View");
					worldview.setBounds(25, 255, 115, 21);
					worldview.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							System.out.println("A");
							roomlistModel=new DefaultComboBoxModel(ai.get_rooms());
							System.out.println("B");
							roomlist.setModel(roomlistModel);
							playerlistModel=new DefaultComboBoxModel(ai.get_players());
							System.out.println("C");
							playerlist.setModel(playerlistModel);
							System.out.println("D");
							itemlistModel=new DefaultComboBoxModel(ai.get_items());
							System.out.println("E");
							itemlist.setModel(itemlistModel);
							System.out.println("F");
							connectionslistModel=new DefaultComboBoxModel(new String[] {});
							System.out.println("G");
							connectionslist.setModel(connectionslistModel);
							System.out.println("H");
							operationslistModel=new DefaultComboBoxModel(defaultoperations);
							System.out.println("I");
							operationslist.setModel(operationslistModel);
							System.out.println("J");
//							System.out.println("worldview.actionPerformed, event="+evt);
							//TODO add your code for worldview.actionPerformed
						}
					});
				}
				{
					help = new JButton();
					panel.add(help);
					help.setText("Help");
					help.setBounds(296, 255, 83, 21);
				}
				{
					logoff = new JButton();
					panel.add(logoff);
					logoff.setText("Log Off");
					logoff.setBounds(176, 255, 87, 21);
					logoff.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							initGUI();
							ai.logoff();
						}
					});
				}
			}
			pack();
			this.setSize(420, 348);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void updateGUI()
	{
		System.out.println("A");
		roomlistModel=new DefaultComboBoxModel(ai.get_rooms());
		System.out.println("B");
		roomlist.setModel(roomlistModel);
		playerlistModel=new DefaultComboBoxModel(ai.get_players());
		System.out.println("C");
		playerlist.setModel(playerlistModel);
		System.out.println("D");
		itemlistModel=new DefaultComboBoxModel(ai.get_items());
		System.out.println("E");
		itemlist.setModel(itemlistModel);
		System.out.println("F");
		connectionslistModel=new DefaultComboBoxModel(new String[] {});
		System.out.println("G");
		connectionslist.setModel(connectionslistModel);
		System.out.println("H");
		operationslistModel=new DefaultComboBoxModel(defaultoperations);
		System.out.println("I");
		operationslist.setModel(operationslistModel);
		System.out.println("J");
	}
	
}
